Bump up error checking and add new spec

Glenn 'devalias' Grant 10 years ago
parent
commit
fe75d6be0a
2 changed files with 24 additions and 7 deletions
  1. 11 6
      app/models/agents/twitter_user_agent.rb
  2. 13 1
      spec/models/agents/twitter_user_agent_spec.rb

+ 11 - 6
app/models/agents/twitter_user_agent.rb

@@ -67,22 +67,27 @@ module Agents
67 67
         options['expected_update_period_in_days'].present?
68 68
         errors.add(:base, "username and expected_update_period_in_days are required")
69 69
       end
70
+
71
+      if options[:include_retweets].present? &&
72
+        !(!!options[:include_retweets] === true || !!options[:include_retweets] === false)
73
+        errors.add(:base, "include_retweets must be a boolean (true/false)")
74
+      end
75
+
76
+      if options[:starting_at].present?
77
+        Time.parse(options[:starting_at]) rescue errors.add(:base, "Error parsing starting_at")
78
+      end
70 79
     end
71 80
 
72 81
     def starting_at
73 82
       if options[:starting_at].present?
74
-        Time.parse(options[:starting_at])
83
+        Time.parse(options[:starting_at]) rescue created_at
75 84
       else
76 85
         created_at
77 86
       end
78 87
     end
79 88
 
80 89
     def include_retweets?
81
-      if options[:include_retweets].present?
82
-        !!options[:include_retweets]
83
-      else
84
-        true
85
-      end
90
+      options[:include_retweets] != false
86 91
     end
87 92
 
88 93
     def check

+ 13 - 1
spec/models/agents/twitter_user_agent_spec.rb

@@ -8,7 +8,7 @@ describe Agents::TwitterUserAgent do
8 8
     @opts = {
9 9
       :username => "tectonic",
10 10
       :expected_update_period_in_days => "2",
11
-      :starting_at => "Jan 01 00:00:01 +0000 2014",
11
+      :starting_at => "Jan 01 00:00:01 +0000 2000",
12 12
       :consumer_key => "---",
13 13
       :consumer_secret => "---",
14 14
       :oauth_token => "---",
@@ -26,4 +26,16 @@ describe Agents::TwitterUserAgent do
26 26
     end
27 27
   end
28 28
 
29
+  describe "#check with starting_at=future date" do
30
+    it "should check for changes starting_at a future date, thus not find any" do
31
+      opts = @opts.merge({ :starting_at => "Jan 01 00:00:01 +0000 2999", })
32
+
33
+      checker = Agents::TwitterUserAgent.new(:name => "tectonic", :options => opts)
34
+      checker.user = users(:bob)
35
+      checker.save!
36
+
37
+      lambda { checker.check }.should change { Event.count }.by(0)
38
+    end
39
+  end
40
+
29 41
 end